﻿
///////////////////////////////////////////////////////////////////////////////////
/// 文 件 名: MaterialsProcurementControl.cs
/// 作    者: XXD
/// 创建日期: 2012/6/18 9:48:18
/// 
///---------------------------------修改记录--------------------------------------
/// 修 改 人:          
/// 修改日期:
/// 修改原因: 
//////////////////////////////////////////////////////////////////////////////////
namespace ZX.WebApp.WorkFlow.CustomForms
{
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI.WebControls;
    using DevExpress.Web.ASPxGridView;
    using ZX.BPM.Library;

    /// <summary>
    /// 采购明细控件
    /// </summary>
    public partial class MaterialsProcurementDetailControl : System.Web.UI.UserControl
    {

        #region 私有方法

        /// <summary>
        /// 设置只读
        /// </summary>
        /// <param name="isRead">是否只读</param>
        void SetReadonly(bool isRead)
        {
            ((GridViewDataTextColumn)gridMaterialsProcurement.Columns["colGoods"]).ReadOnly = isRead;
            ((GridViewDataTextColumn)gridMaterialsProcurement.Columns["colSpecification"]).ReadOnly = isRead;
            ((GridViewDataMemoColumn)gridMaterialsProcurement.Columns["colExplain"]).ReadOnly = isRead;
            ((GridViewDataTextColumn)gridMaterialsProcurement.Columns["colQuantity"]).ReadOnly = isRead;
            ((GridViewDataTextColumn)gridMaterialsProcurement.Columns["colPricesExpected"]).ReadOnly = isRead;
            ((GridViewCommandColumn)gridMaterialsProcurement.Columns["colOperator"]).Visible = !isRead;
 
        }

       
        /// <summary>
        /// 获取字符参数
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>返回字符参数值</returns>
        string GetStringFromQueryString(
            string key,
            string defaultValue)
        {
            string queryStringValue = HttpContext.Current.Request.QueryString[key];
            if (queryStringValue == null)
            {
                return defaultValue;
            }

            return queryStringValue;
        }

        /// <summary>
        /// 添加网格错误信息
        /// </summary>
        /// <param name="errors">列错误信息字典</param>
        /// <param name="column">列</param>
        /// <param name="errorText">错误信息</param>
        void AddError(
            Dictionary<GridViewColumn, string> errors, 
            GridViewColumn column, 
            string errorText)
        {
            if (errors.ContainsKey(column)) return;
            errors[column] = errorText;
        }

        #endregion


        #region 外部接口

        bool readOnly = false;
        /// <summary>
        /// 只读属性
        /// </summary>
        public bool ReadOnly
        {
            set
            {
                SetReadonly(value);
                readOnly = value;
            }
            get
            {
                return readOnly;
            }
        }

        NeedsOfBorrowerEdit dataSource;
        /// <summary>
        /// 当前页面数据源
        /// </summary>
        public NeedsOfBorrowerEdit DataSource
        {
            get
            {
                return dataSource;
            }
            set
            {
                dataSource = value;
            }
        }

        public void EndEdit()
        {
            if (gridMaterialsProcurement.IsEditing)
            {
                gridMaterialsProcurement.EndUpdate();
            }
        }
        #endregion

        #region 事件处理

        protected void ObjectDataSource1_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {
           // NeedsOfBorrowerEdit data = (NeedsOfBorrowerEdit)this.Session[this.GetStringFromQueryString("id", Guid.Empty.ToString())];
            //if (!this.IsPostBack)
            //{
               
            //}
            
            e.ObjectInstance = dataSource;
        }

        protected void grid_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidationEventArgs e)
        {

            if (e.NewValues["Goods"] == null || string.IsNullOrEmpty(e.NewValues["Goods"].ToString()))
            {
                AddError(e.Errors, gridMaterialsProcurement.Columns["Goods"], "采购物品名称必须填写.");
            }

            if (e.NewValues["Specification"] == null || string.IsNullOrEmpty(e.NewValues["Specification"].ToString()))
            {
                AddError(e.Errors, gridMaterialsProcurement.Columns["Specification"], "规格必须填写.");
            }

            int qty = 0;
            int.TryParse(e.NewValues["Quantity"].ToString().ToString(), out qty);
            if (qty == 0)
            {
                AddError(e.Errors, gridMaterialsProcurement.Columns["Quantity"], "数量必须大于零.");
            }


            if (string.IsNullOrEmpty(e.RowError) && e.Errors.Count > 0)
            {
                e.RowError = "请修改所有错误后，再提交.";
            }
        }
      
        protected void grid_StartRowEditing(object sender, DevExpress.Web.Data.ASPxStartRowEditingEventArgs e)
        {
            if (!gridMaterialsProcurement.IsNewRowEditing)
            {
                gridMaterialsProcurement.DoRowValidation();
            }
        }

        protected void grid_ParseValue(object sender, DevExpress.Web.Data.ASPxParseValueEventArgs e)
        {
            if (e.FieldName.Equals("Quantity"))
            {
                int qty = 0;
                if (e.Value == null || !int.TryParse(e.Value.ToString(), out qty))
                {
                    throw new Exception("数量填写不正确。");
                }
            }
        }
        #endregion
    }
}